package me.chan.netty.basic;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class NettyPromiseDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        EventLoop eventLoop = new NioEventLoopGroup().next();

        Promise<String> promise = new DefaultPromise<>(eventLoop);
        eventLoop.submit(() -> {
            try {
                Thread.sleep(1000L);
                promise.setSuccess("All is well");
            } catch (InterruptedException e) {
                log.error(e.toString());
            }

        });

        log.info("waiting for result...");
        log.info("the final result is : {}", promise.get());
    }
}
